<?php

namespace App\Models\Order;

use App\Models\Language;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
 * @deprecated - 此类和 Model/Extension/Extension 重复，合并到 Extension 下面，Extension 不属于 Order
 *
 * Class Extension
 * @property string code
 * @property string type
 * @property string sort_order
 * @property string icon
 * @property string name
 * @property array quote
 * @property string|null geo_zone_id
 * @property string minimum_total
 * @package App\Models\Order
 *
 * @mixin Builder
 */
class Extension extends Model
{
    const LANGUAGE_ID_EN = 1; // 英文的 language_id
    const LANGUAGE_ID_CN = 2; // 中文的 language_id
    const EXTENSION_PAYMENT_TOTAL_DEFAULT = 0; // 支付方式的默认最小值

    const EXTENSION_TYPE_PAYMENT = 'payment';
    const EXTENSION_CODE_FREE_CHECKOUT = 'free_checkout'; // 免费测试
    const SETTING_CODE_PREFIX = 'payment_';
    const SETTING_CODE_SUFFIX = '_status';
    const SETTING_PAYMENT_KEY_TOTAL = '_total';
    const SETTING_KEY_SORT_ORDER = '_sort_order';
    const SETTING_KEY_GEO_ZONE_ID = '_geo_zone_id';
    const SETTING_KEY_WEIGHT_RATE = '_rate';

    const EXTENSION_PAYMENT_ICON_MAP = [
        'pp_express'   => "icon/paypal@2x.png",
        'alipay'       => "icon/zhifubao@2x.png",
        'wechat_pay'   => "icon/weixin@2x.png",
        'cod'          => "icon/icon_huodaofukuan@2x.png",
        'pp_standard'  => "",
        'pp_braintree' => "",
    ];

    const EXTENSION_TYPE_SHIPPING = 'shipping';
    const SETTING_CODE_SHIPPING_PREFIX = 'shipping_';

    const EXTENSION_CODE_SUB_TOTAL = 'sub_total';
    const EXTENSION_TYPE_TOTAL = 'total';
    const SETTING_CODE_TOTAL_PREFIX = 'total_';

    const EXTENSION_TOTAL_CODE_SHIPPING = 'shipping';
    const EXTENSION_TOTAL_CODE_SUBTOTAL = 'sub_total';
    const EXTENSION_TOTAL_CODE_TOTAL = 'total';
    const EXTENSION_TOTAL_CODE_ALLOW = [
        'shipping', 'sub_total', 'total',
    ];

    // 配送的方式
    const EXTENSION_SHIPPING_CODE_FREE = 'free';
    const EXTENSION_SHIPPING_CODE_FLAT = 'flat';
    const EXTENSION_SHIPPING_CODE_WEIGHT = 'weight';
    const EXTENSION_SHIPPING_CODE_PICKUP = 'pickup';

    //支付方式
    const EXTENSION_PAYMENT_COD = 'cod';

    protected $table = 'extension';
    protected $guarded = [];

    /**
     * 返回支付方式的名称
     *
     * @param $code
     * @return null
     */
    public static function getPaymentExtensionName($code)
    {
        $language_code = app()->getLocale() ?? Language::getStoreSettingLanguageCode();

        // 相对于 public/index.php 的地址。
        $path = '../../dw-oc/catalog/language/'.$language_code.'/extension/payment/'.$code.'.php';

        if (is_file($path)) {
            require($path);
        }

        $name = $_['text_title'] ?? null;

        return $name;
    }

    /**
     * 返回统计方式的名称
     *
     * @param $code
     * @return null
     */
    public static function getTotalExtensionName($code)
    {
        $language_code = app()->getLocale() ?? Language::getStoreSettingLanguageCode();

        // 相对于 public/index.php 的地址。
        $path = '../../dw-oc/catalog/language/'.$language_code.'/extension/total/'.$code.'.php';

        if (is_file($path)) {
            require($path);
        }

        $name = $_['text_'.$code] ?? null;

        return $name;
    }

    /**
     * 返回配送方式的名称
     *
     * @param $code
     * @return null
     */
    public static function getShippingExtensionName($code)
    {
        $language_code = app()->getLocale() ?? Language::getStoreSettingLanguageCode();

        // 相对于 public/index.php 的地址。
        $path = '../../dw-oc/catalog/language/'.$language_code.'/extension/shipping/'.$code.'.php';

        if (is_file($path)) {
            require($path);
        }

        $name = $_['text_title'] ?? null;

        return $name;
    }
}
